"""
大陆的霸者 - SL花田专用脚本
使用需设置分辨率为1280*720分辨率
Mumu模拟器使用效果最佳，如需使用其他模拟器请自行修改端口并重新build脚本
"""

import sys
import time
from adb_shell.adb_device import AdbDeviceTcp

print("大陆的霸者 - SL花田专用脚本", end='\n')
print("请移动到花田最左侧开启脚本", end='\n')
print("模式1 - 薅左侧果实", end='\n')
print("模式2 - 薅右侧果实", end='\n')
mode = input("请输入模式:")
device = AdbDeviceTcp('127.0.0.1', 7555, default_transport_timeout_s=9.)
device.connect(rsa_keys=[], auth_timeout_s=0.1)
# device.root()

stat = device.exec_out("wm size")
if "1280x720" not in stat:
    print("分辨率不是1280x720，请调整后重试。")
    sys.exit(1)


def tap(x, y):
    device.exec_out('input tap ' + str(x) + ' ' + str(y))


def get_pixel_color(x, y):
    device.exec_out('screencap /sdcard/screen.dump')
    return device.exec_out('dd if="/sdcard/screen.dump" bs=4 count=1 skip='+str(3+1280*y+x)+' 2>/dev/null', decode=False).hex()[0:6]


def get_pixel_color_without_cropping(x, y):
    return device.exec_out('dd if="/sdcard/screen.dump" bs=4 count=1 skip='+str(3+1280*y+x)+' 2>/dev/null', decode=False).hex()[0:6]


def is_minimap_exist():
    c1 = get_pixel_color(1078, 137)
    c2 = get_pixel_color_without_cropping(1079, 137)
    return True if c1 == c2 == 'e2d57e' else False


def is_screen_white():
    c1 = get_pixel_color(0, 0)
    return True if c1 == 'ffffff' else False


def is_screen_black():
    c1 = get_pixel_color(0, 0)
    return True if c1 == '000000' or c1 == '010101' else False


def restart_cotc():
    print("关闭游戏App")
    device.exec_out('am force-stop com.boltrend.octopath.tc')
    time.sleep(2)
    print("启动游戏App")
    device.exec_out('am start com.boltrend.octopath.tc/com.epicgames.ue4.GameActivity')
    time.sleep(2)

    print("等待游戏加载……")
    while True:
        time.sleep(2)
        if is_screen_white():
            break

    print("等待游戏登录……")
    while True:
        time.sleep(1)
        if is_screen_black():
            break
        else:
            for i in range(10):
                time.sleep(0.2)
                tap(10, 10)

    print("等待游戏进入……")
    while True:
        time.sleep(1)
        if is_minimap_exist():
            break


def is_item_obtained():
    device.exec_out("screencap /sdcard/screen.dump")
    out = device.exec_out("dd if='/sdcard/screen.dump' bs=4 count=16 skip=362896", decode=False).hex()
    return out == "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"


def is_6_exist_by_pixel():
    c = get_pixel_color(710, 336)
    return True if c == 'f0ece7' else False


def sl_item():
    time.sleep(0.5)
    if mode == "1":
        print("点击左侧花田采集点……")
        tap(760, 350)
    elif mode == "2":
        print("点击右侧花田采集点……")
        tap(930, 350)
    else:
        print("不支持的模式，退出脚本……")
        sys.exit(1)

    print("检查获取物品……")
    counter = 0
    while True:
        if counter > 10:
            print("点击对象有误，脚本结束。")
            sys.exit(1)
        time.sleep(0.3)
        if is_item_obtained():
            time.sleep(0.2)
            break
        else:
            counter += 1
            continue
    if is_6_exist_by_pixel():
        print("--获取6/9秘技果实--")
        sys.exit(1)
        # tap(640, 433)
        # time.sleep(1)
    else:
        print("--获取失败--")
        restart_cotc()
    sl_item()


try:
    sl_item()
except SystemExit:
    input("按任意键关闭窗口……")
